package net.penguinmenac3.math.rings;

import net.penguinmenac3.math.MathObject;

/**
 * Represents the Group of real numbers.
 * 
 * @author Michael
 */
public class RealNumber implements MathObject {
	private double value;

	public RealNumber(double value) {
		this.value = value;
	}

	@Override
	public MathObject add(MathObject o) {
		RealNumber r = (RealNumber) o;
		return new RealNumber(this.value + r.value);
	}

	@Override
	public MathObject subtract(MathObject o) {
		RealNumber r = (RealNumber) o;
		return new RealNumber(this.value - r.value);
	}

	@Override
	public MathObject multiply(MathObject o) {
		RealNumber r = (RealNumber) o;
		return new RealNumber(this.value * r.value);
	}

	@Override
	public MathObject divide(MathObject o) {
		if (isNullElement()) {
			return new RealNumber(0);
		}
		RealNumber r = (RealNumber) o;
		return new RealNumber(this.value / r.value);
	}

	@Override
	public boolean isNeutralElement() {
		if (value < 1.000000001 && value > 0.999999999) {
			return true;
		}
		return false;
	}
	
	@Override
	public boolean isNullElement() {
		if (value < 0.000000001 && value > -0.000000001) {
			return true;
		}
		return false;
	}

	@Override
	public RealNumber clone() {
		return new RealNumber(value);
	}

	@Override
	public String toString() {
		if (isNeutralElement()) {
			return "1.0";
		}
		if (isNullElement()) {
			return "0.0";
		}
		return String.valueOf(value);
	}
	
	@Override
	public String getValue() {
		if (isNeutralElement()) {
			return "1.0";
		}
		if (isNullElement()) {
			return "0.0";
		}
		return String.valueOf(value);
	}
	
	@Override
	public MathObject inverse() {
		if (isNullElement()) {
			throw new IllegalArgumentException("ZERO has no inverse");
		}
		return new RealNumber(1/value);
	}
	
	@Override
	public MathObject additiveInverse() {
		return new RealNumber(-value);
	}
}
